
var thisurl = top.window.location.href;
function getItemid(){
	return thisurl.split("?")[1].split("&")[0].split("=")[1];	
}
$(document).ready(function() {
	getItem();
	getcomm(0);
});

function ShowItem(data){
//	alert("callback ok"+ data.msg);
	if(data.msg !="ok"){
		showmsg(data.msg);
	}else{
//		alert(data.res);
		$("#item_img")[0].src=data.res.img;
		$("#itemattr").append("<span style='font-weight:800;font-size:20px;'>"+data.res.name
				+"</span><br/><br/><span style='font-weight:600;'>人物标签：</span><span style='color:#69A3C9; font-style:italic;'>"
				+tagsplit(data.res.tags)+"</span>");
		$("#sharelink").append("分享到：<a id=\"sina\" class=\"menu\" href=\"javascript:window.open('http://v.t.sina.com.cn/share/share.php?appkey=&url='+encodeURIComponent('"+thisurl+"')+'&pic='+encodeURIComponent('"+data.res.img+"')+'&title='+encodeURIComponent('有关"+data.res.name+"的个人评价'),'_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes'); void 0\"><img src='http://t.sina.com.cn/favicon.ico' alt='新浪微博分享' border='0' width=16 height=16>新浪微博</a>");
		$("#sharelink").append("<a id=\"qq\" class=\"menu\" href=\"javascript:window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+encodeURIComponent('"+thisurl+"')+'&title='+encodeURIComponent('有关"+data.res.name+"的个人评价'),'_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes'); void 0\"><img src='http://qzone.qq.com/favicon.ico' alt='QQ空间分享' border='0' width=16 height=16>QQ空间</a> ");
		$("#sharelink").append("<a id=\"tqq\" class=\"menu\" href=\"javascript:window.open('http://v.t.qq.com/share/share.php?url='+encodeURIComponent('"+thisurl+"')+'&pic='+encodeURIComponent('"+data.res.img+"')+'&title='+encodeURIComponent('有关"+data.res.name+"的个人评价'),'_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes'); void 0\"><img src='http://t.qq.com/favicon.ico' alt='腾讯微博分享' border='0' width=16 height=16/>腾讯微博</a> ");
	}
}
function getItem(){
$.ajax({
	type:"POST",
	url:"ShowItem",
	dataType:"json",
	success:ShowItem,
	data:"itemid="+getItemid(),
	error : function(data){
		showmsg("get Item Error");
	}
});

}

function tagsplit(str){
	var s=str.split(" ");
	var rsp="";
	for(var t in s){
		if (t==s.length-1)
			rsp+=s[t];
		else
			rsp+=s[t]+" / ";
	}
	return rsp;
}

function getcomm(pageNo){
$.ajax({
	type:"POST",
	url:"GetComments",
	dataType:"json",
	success:showComments,
	data:"itemid="+getItemid()+"&page="+pageNo,
	error : function(data){
		showmsg("系统错误，请稍后再试！");
	}
});

}

function showComments(data){
	commentCount=parseInt(data.msg);
	if(commentCount>0){
		$("#comments").append("<label style='color:green;font-weight:600;'>对他的评价</label>"+"（"+commentCount+"）");
		if(data.res){
			for(var i=0;i<data.res.length;i++){
//				$("#comments").append("<div class='comment'>"+data.res[i].content+"<br> <br> <div class='rightcorner'>+赞同人数：  <span class='voteno' id='voteno"+data.res[i].id+"_"+data.res[i].uid+"'>"+data.res[i].vote+"</span>   <input type='button' value='赞同'   onclick=\"vote('"+data.res[i].id+"_"+data.res[i].uid+"')\"/><br/>评论者："+data.res[i].uname+"<br/> 评论时间："+data.res[i].ctime+"</div></div>");

				$("#comments").append("<div class='comment'><div class='comment_head'><label class='uname'>"+data.res[i].uname
						+"</label><label class='time'> "+data.res[i].ctime+"</label><font style='font-style:italic;'> 评论到：</font></div><div class='comment_content'><label class='comment'>" +
								data.res[i].content+"</label></div>"
								+"<div class='rightcorner'> <span class='voteno' id='voteno"+data.res[i].id+"_"+data.res[i].uid+"' onclick=\"vote('"+data.res[i].id+"_"+data.res[i].uid+"')\">支持（已有"+data.res[i].vote+"人支持）</span></div></div>" );
			
			}
		}else{
		}
		
	}else if(commentCount==0){
		$("#comments").append("<label style='color:green;font-weight:600;'>对他的评价</label>"+"（"+commentCount+"）");
		$("#comments").append("<div class='comment'>沙发空缺中，快来抢啊！</comment>");
	}else {
		showmsg("系统问题，请稍后再试！");
	}
}



function addcom(){
	var userid = getCookie("user");
	if(!userid){
		showmsg("login please");
	}else{
		$("#addcm").show();
	}
}

function submitcom(){
	function callback(data){
		var rcode=parseInt(data.result);
		var msg="";
		if(rcode>0){
			$("#comments").append("<div class='comment'>"+$("#content")[0].value+"<br><br> <div class='rightcorner'> 我刚发表的评论</div></div>");
			$("#content")[0].value="";
			return;
		}
		else if(-1==rcode){
			showLoginWin();
			loginwarn("您尚未登录，请先登录后再发表评论，谢谢！");
			return;
		}
		else if(-2==rcode){
			msg="抱歉，系统正在维护中，请您稍后再试！";
		}
		else if(-3==rcode){
			msg="抱歉，您输入的内容不合法！";
		}
		else if(-4==rcode){
			msg="抱歉，您对同一人的评论不能超过3条！";
		}
		else if(-5==rcode){
			msg="抱歉，您评论的内容超过2000字了！";
		}
		
		
		showmsg(msg);	
		
	}
	$.ajax({
		type:"POST",
		url:"AddComment",
		dataType:"json",
		success:callback,
		data:"itemid="+getItemid()+"&content="+$("#content").val(),
		error : function(data){
			alert("add comment Error.please try again");
		}
	});
}

function vote(id){
	
	function callback(data){
		ret=parseInt(data.res);
		if( 0==ret){
			showLoginWin();
			loginwarn("您尚未登录，请先登录后再操作，谢谢！");
			return;
		}
		else if(ret>0){
			var vn = $("#voteno"+id).text();
			$("#voteno"+id).html(vn*1+1);
		}
		else if(ret<0){
			showmsg(data.msg);
		}
	}
	
	$.ajax({
		type:"POST",
		url:"AgreeView",
		dataType:"json",
		success:callback,
		data:"commentid="+id+"&itemid="+getItemid(),
		error : function(data){
			alert("vote Error.please try again later");
		}
	});
}


function showmsg(message){
	showTop(message, 500, 200, "35%");
}




